package com.y4oung.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.y4oung.util.ResponseJsonUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.y4oung.model.Business;
import com.y4oung.model.BusinessNew;
import com.y4oung.service.IBusinessService;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/business")

public class BusinessController {
	@Resource
	private IBusinessService businessService;
	
	@RequestMapping(value="/showBusinessList.do",produces="text/plain;charset=utf-8")
    public void showBusinessList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        List<Business> businessList = this.businessService.showBusinessList();        
        ObjectMapper mapper = new ObjectMapper();
        for (Business business : businessList) {
        	response.getWriter().write(mapper.writeValueAsString(business));
        }
        response.getWriter().close();
    }
	
	@RequestMapping("/showBusiness.do")
	//读取单个餐厅详细信息，输入餐厅id为int
	public void showBusiness(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int businessId = Integer.parseInt(request.getParameter("businessId"));
        Business business = this.businessService.showBusiness(businessId);
        List<BusinessNew> businessNewList = this.businessService.showBusinessNew(businessId);        
        ObjectMapper mapper = new ObjectMapper();
        response.getWriter().write(mapper.writeValueAsString(business));
        for (BusinessNew businessNew : businessNewList) {
        	response.getWriter().write(mapper.writeValueAsString(sdf.format(businessNew.getDate())+businessNew.getText()));
        }
        response.getWriter().close();
    }

    @ResponseBody
    @RequestMapping("/getGallery.do")
    public void getGallery(HttpServletRequest request, HttpServletResponse response){
	    List<Business> list  = businessService.showBusinessList();
        JSONObject json = new JSONObject();
        json.put("list",list);
        ResponseJsonUtils.json(response,json);
    }

    @RequestMapping("/showByColumns.do")
    //根据标签显示餐厅列表，输入cuisine为菜系，region为区域，type为氛围，如果不选择此标签则标签内容为空
    public void showByColumns(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        String cuisine = request.getParameter("cuisine");
        String region = request.getParameter("region");
        String type = request.getParameter("type");

        List<Business> businessList = this.businessService.showByColumns(cuisine,region,type);
        ObjectMapper mapper = new ObjectMapper();
        for (Business business : businessList) {
            response.getWriter().write(mapper.writeValueAsString(business));
        }
        response.getWriter().close();
    }

    @RequestMapping("/orderByColumns.do")
    //排序，输入columns为需要排序的字段名，order为升序ASC或降序DESC
    public void orderByColumns(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        Map<String, Object> map = new HashMap<String, Object>();

        String columns = request.getParameter("columns");
        String order = request.getParameter("order");
        map.put("columns", columns);
        map.put("order", order);

        List<Business> businessList = this.businessService.orderByColumns(map);
        ObjectMapper mapper = new ObjectMapper();
        for (Business business : businessList) {
            response.getWriter().write(mapper.writeValueAsString(business));
        }
        response.getWriter().close();
    }

    @RequestMapping("/searchBusiness.do")
    //关键字搜索，输入columns为所需匹配字段名，keyword为输入关键字
    public void searchBusiness(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        Map<String, Object> map = new HashMap<String, Object>();
        String columns = request.getParameter("columns");
        String keyword = "'%"+request.getParameter("keyword")+"%'";
        map.put("columns", columns);
        map.put("keyword", keyword);

        List<Business> businessList = this.businessService.searchBusiness(map);
        ObjectMapper mapper = new ObjectMapper();
        for (Business business : businessList) {
            response.getWriter().write(mapper.writeValueAsString(business));
        }
        response.getWriter().close();
    }


    @RequestMapping("getPopular")
    public String getPopular(HttpServletRequest request, HttpServletResponse response) {
        return "restaurantsGallery";
    }

    @RequestMapping("getNew")
    public String getNew(HttpServletRequest request, HttpServletResponse response) {
        return "restaurantsGallery";
    }

    @RequestMapping("getHealth")
    public String getHealth(HttpServletRequest request, HttpServletResponse response) {
        return "restaurantsGallery";
    }
}
